package com.baosight.demo;

import com.alibaba.fastjson.JSONObject;
import com.baosight.demo.bean.MethodInfo;
import com.baosight.demo.visitor.MethodVisitor;
import com.baosight.ice.annotation.IceMark;
//import com.baosight.ice.util.ConfLoader;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.visitor.VoidVisitor;
import com.zeroc.Ice.Current;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.baosight.ice.IceBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;


@SpringBootApplication
@ComponentScan(basePackages = "com.baosight")
public class SpringbootdemoApplication extends IceBootApplication{



    public static void main(String[] args) {
        port=19988;
        localRun(SpringbootdemoApplication.class,args);

    }

    public static void getMethodName(){
        Map<String, Object> beansWithAnnotation =  app.getBeansWithAnnotation(IceMark.class);
        System.out.println(beansWithAnnotation);
        for(Map.Entry<String, Object> entry : beansWithAnnotation.entrySet()){
            Object obj = entry.getValue();
            //获取实例对象的注解
            IceMark annotation = obj.getClass().getAnnotation(IceMark.class);
            //定义的代理服务ID
            String serviceId = annotation.identity();
            //获取类
            Class classz=obj.getClass();
            System.out.println(classz.getFields());
            String className=classz.getName();
            logger.info("class "+className);
            Method[] declaredMethods = obj.getClass().getDeclaredMethods();
            for(Method method:declaredMethods){
                logger.info("method "+method.getName());
                for(Parameter param:method.getParameters()){
                    logger.info("param "+param.getName()+" "+param.getType()+" "+(param.getType()==Current.class));
                }
            }
        }
    }

    public static void getComment() throws FileNotFoundException {
        String baseFile="C:\\ideaProject\\springbootdemo\\src\\main\\java\\";
        Map<String, Object> beansWithAnnotation =  app.getBeansWithAnnotation(IceMark.class);
        System.out.println(beansWithAnnotation);
        for(Map.Entry<String, Object> entry : beansWithAnnotation.entrySet()){
            Object obj = entry.getValue();
            //获取实例对象的注解
            IceMark annotation = obj.getClass().getAnnotation(IceMark.class);
            //定义的代理服务ID
            String serviceId = annotation.identity();
            //获取类
            Class classz=obj.getClass();
            System.out.println(classz.getFields());
            String className=classz.getName();
            String javaFile=baseFile+className.replace(".","\\")+".java";
            JSONObject jsonObject = parseClass(classz, javaFile);
        }
    }

    public static JSONObject parseClass(Class<?> classz,String javaFile) throws FileNotFoundException {
        JSONObject jsonObject = new JSONObject();
        CompilationUnit cu = StaticJavaParser.parse(new File(javaFile));
        List<MethodInfo> methodInfoList=new ArrayList<>();
        VoidVisitor<List<MethodInfo>> methodNameVisitor=new MethodVisitor();
        methodNameVisitor.visit(cu,methodInfoList);

        return jsonObject;
    }

}
